<?php
/**
 * Function:
 * Description:
 * Abo 2018/10/10 17:33
 * Email: abo2013@foxmail.com
 */

//创建Server对象，监听 127.0.0.1:9502端口，类型为SWOOLE_SOCK_UDP
$serv = new swoole_server("127.0.0.1", 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);

//监听数据接收事件
$serv->on('Packet', function ( $serv, $data, $clientInfo ) {
    // 独播
    //$serv->sendto( $clientInfo['address'], $clientInfo['port'], "Server ".$data );

    // 广播
    $_SESSION[ 'clients' ][ "{$clientInfo['address']}{$clientInfo['port']}" ] = $clientInfo;
    foreach ( $_SESSION[ 'clients' ] as $v2client ) {
        $serv->sendto( $v2client['address'], $v2client['port'], "UDP Server ".$data );
    }
    var_dump( $_SESSION[ 'clients' ] );

});



//启动服务器
$serv->start();